import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Brand, Detail, Part} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus, productCategoryType} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '二次索赔单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

const partDesc = formatMessage({
    id: 'api.message.dealerDesc',
    defaultMessage: '查询备件'
});
const getUpdateListDesc = formatMessage({
    id: 'api.message.getUpdateListDesc',
    defaultMessage: '查询二次索赔单'
});
const updateAllDataDesc = formatMessage({
    id: 'api.message.updateAllDataDesc',
    defaultMessage: '批量编辑'
});
const workItemsQueryDesc = formatMessage({
    id: 'api.message.workItemsQueryDesc',
    defaultMessage: '查询维修项目'
});
const getPartServicePricesDesc = formatMessage({
    id: 'api.message.getPartServicePricesDesc',
    defaultMessage: '查询索赔材料价格'
});

export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, brandDesc));
};

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/secondClaims/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/secondClaims/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/secondClaims/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询备件信息
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partDesc));
};

// 查询批量调整的二次索赔单
export const getUpdateList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/secondClaims/forUpdate?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getUpdateListDesc));
};
// 查询选中的批量调整的二次索赔单
export const getUpdateDetailList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/secondClaims/forUpdate/details?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getUpdateListDesc));
};
//批量调整
export const updateAllData = data => request(`${baseUrl}/secondClaims/update`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateAllDataDesc));

const productCategoryQueryDesc = formatMessage({
    id: 'api.message.productCategoryQueryDesc',
    defaultMessage: '查询车型信息'
});
export const productCategoryQuery = () =>
    request(`${baseUrl}/productCategories?type=${productCategoryType.车型}&isAll=true`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryQueryDesc));

const partsQueryDesc = formatMessage({
    id: 'api.message.partsQueryDesc',
    defaultMessage: '查询维修辅料'
});
const getMgmtRatesDesc = formatMessage({
    id: 'api.message.getMgmtRatesDesc',
    defaultMessage: '管理费率查询'
});
const getWorkItemsDesc = formatMessage({
    id: 'api.getWorkItemsDesc',
    defaultMessage: '查询维修项目'
});
//查询辅料
export const partAssistQuery = data => request(`${baseUrl}/parts/query`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, partsQueryDesc));

// 查询万能工时
export const getWorkItems = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/workItems/forClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getWorkItemsDesc));
};
//管理费率
export const getMgmtRates = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/mgmtRates?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMgmtRatesDesc));
};
//查询维修项目
export const workItemsQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/workItems/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, workItemsQueryDesc));
};
//索赔材料价格查询
export const getPartServicePrices = data => request(`${baseUrl}/partServicePrices/forClaim`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.query, getPartServicePricesDesc));

